package com.pojoin.cms.action;

import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;

import com.pojoin.cms.domain.Resource;
import com.pojoin.cms.domain.Role;
import com.pojoin.cms.domain.form.RoleForm;
import com.pojoin.cms.exception.CmsException;
import com.pojoin.cms.face.RoleFace;
import com.pojoin.cms.face.RoleFaceFactory;
import com.pojoin.cms.service.ResourceService;
import com.pojoin.cms.service.RoleService;

@SuppressWarnings("serial")
public class RoleAction extends PagerAction {
	
	private RoleService roleService;
	private ResourceService resourceService;
	private RoleFaceFactory roleFaceFactory;
	private RoleForm role;
	private List<Role> list;
	private List<Resource> resourceList;
	
	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public RoleFaceFactory getRoleFaceFactory() {
		return roleFaceFactory;
	}

	public void setRoleFaceFactory(RoleFaceFactory roleFaceFactory) {
		this.roleFaceFactory = roleFaceFactory;
	}

	public RoleForm getRole() {
		return role;
	}

	public void setRole(RoleForm role) {
		this.role = role;
	}

	public List<Role> getList() {
		return list;
	}

	public void setList(List<Role> list) {
		this.list = list;
	}

	public List<Resource> getResourceList() {
		return resourceList;
	}

	public void setResourceList(List<Resource> resourceList) {
		this.resourceList = resourceList;
	}
	
	

	/**
	 * 请求新增角色
	 * @return
	 */
	public String willAdd(){
		this.resourceList = this.resourceService.getAllResources();
		return this.success("role/add.jsp");
	}
	
	/**
	 * 新增
	 * @return
	 * @throws CmsException 
	 */
	public String add() throws CmsException{
		Role r = this.roleService.add(this.role);
		if(r == null){
			this.msg = "角色已存在";
			return this.willAdd();
		}
		RoleFace rf = this.roleFaceFactory.getRoleFaceById(r.getRoleId());
		rf.resetResources(this.role.getSelectedResourcesIds());
		rf.flush();
		return this.list();
	}
	
	public String del() throws CmsException{
		RoleFace rf = this.roleFaceFactory.getRoleFaceById(this.role.getRoleId());
		rf.delete();
		return this.list();
	}
	
	/**
	 * 加载角色对象
	 * @return
	 */
	public String load(){
		this.resourceList = this.resourceService.getAllResources();
		Role r = this.roleService.findById(this.role.getRoleId());
		BeanUtils.copyProperties(r, this.role);
		Set<Resource> rcset = r.getResources();
		String selectedResourcesIds = "";
		for(Resource rc : rcset){
			selectedResourcesIds += rc.getResourceId() + ",";
		}
		this.role.setSelectedResourcesIds(selectedResourcesIds);
		
		return this.success("role/update.jsp");
	}
	
	public String update() throws CmsException{
		RoleFace roleFace = this.roleFaceFactory.getRoleFaceById(this.role.getRoleId());
		roleFace.update(this.role);
		roleFace.flush();
		return this.list();
	}
	
	/**
	 * 获取列表页面
	 * @return
	 */
	public String list(){
		this.list = this.roleService.listAllRoles(this.pager);
		return this.success("role/list.jsp");
	}
}
